<template>
	<view v-if="data.length !== 0">
		<view class="item" v-for="item in bianliData" :key="item.id">
			<view class="top">
				<view class="left">
					<uni-icons type="person" size="20" color="grey"></uni-icons>
					<text style="margin: 0 10upx;">{{item.shopName}}</text>
					<uni-icons type="right" size="20" color="grey"></uni-icons>
				</view>
				<text style="color: rgb(35,209,208);" v-if='item.shopState == 1'>交易完成</text>
				<text style="color: rgb(35,209,208);" v-if='item.shopState == 2'>交易完成</text>
				<text style="color: rgb(35,209,208);" v-if='item.shopState == 3'>未评价</text>
				<text style="color: rgb(35,209,208);" v-if='item.shopState == 4'>已交易/未交易</text>
			</view>
			<view class="content">
				<!-- <text>{{item.shopImg}}</text> -->
				<image :src="item.shopImg" mode="widthFix" :class="{'kongImg': !item.shopImg}"></image>
				<view class="right">
					<view class="top">
						<text style="width: 70%;">{{item.shopTitle}}</text>
						<text style="width: 25%; font-size: 24upx;">￥{{item.shopPrice}}.00</text>
					</view>
					<view class="btnBox">
						<el-button class='btn' v-if='item.shopState == 1' @click='state1(item)'>未签收</el-button>
						<el-button class='btn' v-if='item.shopState == 2'>已签收</el-button>
						<el-button class='btn' v-if='item.shopState == 3'>订单评价</el-button>
						<el-button class='btn' v-if='item.shopState == 4'>查看详情</el-button>
					</view>
				</view>
			</view>
		</view>
	</view>
	<view class="showView" v-else>
		暂无购买记录
	</view>
</template>

<script>
	export default {
		props: ['index'],
		data() {
			return {
				data: [],
				bianliData: [],
				userId: 0
			};
		},
		methods: {
			state1(item) {
				console.log(item)
				item.shopState = '2'
				this.$api.putIBoughtIt(item).then(res => {
					console.log(res)
					if (res.code == 200) {
						if (this.index == 1) {
							this.$api.getIBoughtIt().then(res => {
								// console.log(res)
								if (res.code == 200) {
									this.data = res.data
								}
							})
						} else {
							this.$api.getIBoughtItShopState(this.index).then(res => {
								// console.log(res)
								if (res.code == 200) {
									this.data = res.data
								}
							})
						}
					}
				})
			}
		},
		watch: {
			index: {
				handler(newValue, oldValue) {
					const userId = getApp().globalData.userId
					this.userId = userId
					if (newValue == 1) {
						this.$api.getIBoughtItUserId(this.userId).then(res => {
							// console.log(res)
							if (res.code == 200) {
								this.data = res.data
								this.bianliData = res.data
							}
						})
					} else if (newValue != 1) {
						this.$api.getIBoughtItUserId(this.userId).then(res => {
							// console.log(res)
							if (res.code == 200) {
								this.data = res.data
								this.bianliData = res.data.filter(item => item.shopState == newValue)
							}
						})
					}
				},
				immediate: true,
				deep: true, // 深度监听父组件传过来对象变化 
			}
		}
	}
</script>

<style lang="scss">
	.kongImg {
		height: 0;
	}

	.item {
		width: 90%;
		// height: 20vh;
		margin: 40upx auto;
		box-sizing: border-box;
		padding: 3%;
		border-radius: 20upx;
		box-shadow: 0px 0px 18upx rgba(0, 0, 0, 0.1);
		display: flex;
		flex-direction: column;
		justify-content: space-around;

		.top {
			width: 100%;
			display: flex;
			justify-content: space-between;
			font-size: 26upx;
			margin-bottom: 20upx;

			.left {
				display: flex;
				align-items: center;
			}
		}

		.content {
			width: 100%;
			font-size: 26upx;
			display: flex;

			image {
				width: 18%;
				margin-right: 20upx;
			}

			.right {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.top {
					display: flex;
				}
			}

			.btnBox {
				display: flex;
				justify-content: flex-end;

				.btn {
					padding: 10upx 40upx;
					font-size: 24upx;
					border-radius: 30upx;
					border-color: rgb(35, 209, 208);
					color: rgb(35, 209, 208);
				}
			}
		}
	}

	.showView {
		text-align: center;
		margin: 400upx auto 0;
	}
</style>
